﻿using ECGConversion;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace HnECGConverter2
{
    public partial class Form1 : Form
    {
		private int _Zoom = 1;
		private int _OffsetX = 0;
		private int _OffsetY = 0;
		private int _PrevX = 0;
		private int _PrevY = 0;
		private int _LineX1 = int.MinValue;
		private int _LineX2 = int.MinValue;
		private int _LineY1 = int.MinValue;
		private int _LineY2 = int.MinValue;
		private bool _RightMouseButton = false;


		private IECGFormat _CurrentECG = null;
		//private IECGFormat CurrentECG
		//{
		//	get
		//	{
		//		lock (this)
		//		{
		//			return _CurrentECG;
		//		}
		//	}
		//	set
		//	{
		//		lock (this)
		//		{
		//			_Zoom = 1;

		//			_OffsetX = 0;
		//			_OffsetY = 0;

		//			if ((_CurrentECG != null)
		//			&& (_CurrentECG != value))
		//				_CurrentECG.Dispose();

		//			if (value == null)
		//			{
		//				if (_CurrentECG != null)
		//					_CurrentECG.Dispose();

		//				_CurrentECG = null;
		//			}
		//			else
		//			{
		//				_CurrentECG = value;

		//				if (_CurrentECG.Signals.getSignals(out _CurrentSignal) != 0)
		//				{
		//					this.statusBar.Text = "Failed to get signal!";

		//					_CurrentECG.Dispose();
		//					_CurrentECG = null;
		//				}
		//				else
		//				{
		//					if (_CurrentSignal != null)
		//					{
		//						for (int i = 0, e = _CurrentSignal.NrLeads; i < e; i++)
		//						{
		//							ECGTool.NormalizeSignal(_CurrentSignal[i].Rhythm, _CurrentSignal.RhythmSamplesPerSecond);
		//						}
		//					}

		//					Signals sig = _CurrentSignal.CalculateTwelveLeads();
		//					if (sig == null)
		//						sig = _CurrentSignal.CalculateFifteenLeads();

		//					if (sig != null)
		//						_CurrentSignal = sig;

		//					if (_CurrentSignal.IsBuffered)
		//					{
		//						BufferedSignals bs = _CurrentSignal.AsBufferedSignals;

		//						bs.LoadSignal(bs.RealRhythmStart, bs.RealRhythmStart + 60 * bs.RhythmSamplesPerSecond);

		//						ECGTimeScrollbar.Minimum = 0;
		//						ECGTimeScrollbar.Maximum = bs.RealRhythmEnd - bs.RealRhythmStart;
		//						ECGTimeScrollbar.Value = 0;
		//						ECGTimeScrollbar.SmallChange = _CurrentSignal.RhythmSamplesPerSecond;
		//						ECGTimeScrollbar.LargeChange = _CurrentSignal.RhythmSamplesPerSecond;
		//					}
		//					else
		//					{
		//						int start, end;

		//						_CurrentSignal.CalculateStartAndEnd(out start, out end);

		//						ECGTimeScrollbar.Minimum = 0;
		//						ECGTimeScrollbar.Maximum = end - start;
		//						ECGTimeScrollbar.Value = 0;
		//						ECGTimeScrollbar.SmallChange = _CurrentSignal.RhythmSamplesPerSecond;
		//						ECGTimeScrollbar.LargeChange = _CurrentSignal.RhythmSamplesPerSecond;
		//					}
		//				}

		//				ECGDraw.ECGDrawType dt = ECGDraw.PossibleDrawTypes(_CurrentSignal);

		//				menuLeadFormatRegular.Enabled = (dt & ECGDraw.ECGDrawType.Regular) != 0;
		//				menuLeadFormatThreeXFour.Enabled = (dt & ECGDraw.ECGDrawType.ThreeXFour) != 0;
		//				menuLeadFormatThreeXFourPlusOne.Enabled = (dt & ECGDraw.ECGDrawType.ThreeXFourPlusOne) != 0;
		//				menuLeadFormatThreeXFourPlusThree.Enabled = (dt & ECGDraw.ECGDrawType.ThreeXFourPlusThree) != 0;
		//				menuLeadFormatSixXTwo.Enabled = (dt & ECGDraw.ECGDrawType.SixXTwo) != 0;
		//				menuLeadFormatMedian.Enabled = (dt & ECGDraw.ECGDrawType.Median) != 0;

		//				if ((menuLeadFormatThreeXFour.Checked && !menuLeadFormatThreeXFour.Enabled)
		//				|| (menuLeadFormatThreeXFourPlusOne.Checked && !menuLeadFormatThreeXFourPlusOne.Enabled)
		//				|| (menuLeadFormatThreeXFourPlusThree.Checked && !menuLeadFormatThreeXFourPlusThree.Enabled)
		//				|| (menuLeadFormatSixXTwo.Checked && !menuLeadFormatSixXTwo.Enabled)
		//				|| (menuLeadFormatMedian.Checked && !menuLeadFormatMedian.Enabled))
		//				{
		//					CheckLeadFormat(ECGDraw.ECGDrawType.Regular, false);
		//				}
		//			}

		//			_DrawBuffer = null;
		//		}
		//	}
		//}

		public Form1()
        {
            InitializeComponent();
            //try
            //{
            //    StringBuilder sb = new StringBuilder();

            //    sb.Append("Current Format (*.*)|*.*");

            //    int i = 0;

            //    string[] supportedList = ECGConverter.Instance.getSupportedFormatsList();

            //    foreach (string format in supportedList)
            //    {
            //        string extension = ECGConverter.Instance.getExtension(i++);

            //        sb.Append('|');

            //        sb.Append(format);
            //        sb.Append(" File");

            //        if (extension == null)
            //            extension = "ecg";

            //        sb.Append(" (*.");
            //        sb.Append(extension);
            //        sb.Append(")|*.");
            //        sb.Append(extension);
            //    }

            //    if (true)
            //    {
            //        int index = 0;

            //        IECGFormat writeFile = CurrentECG;

            //        if (index >= 0)
            //        {
            //            ECGConfig cfg = ECGConverter.Instance.getConfig(index);

            //            if (cfg != null)
            //            {
            //                cfg["Lead Format"] = "Regular";
            //                cfg["Gain"] = "10";
            //            }



            //            try
            //            {
            //                if (CurrentECG.GetType() != ECGConverter.Instance.getType(index))
            //                {
            //                    if (((ECGConverter.Instance.Convert(CurrentECG, index, cfg, out writeFile) != 0)
            //                    || !writeFile.Works())
            //                    && (writeFile != null))
            //                    {
            //                        writeFile.Dispose();
            //                        writeFile = null;
            //                    }
            //                }
            //            }
            //            catch
            //            {
            //                if (writeFile != null)
            //                {
            //                    writeFile.Dispose();
            //                    writeFile = null;
            //                }
            //            }

            //            if (writeFile == null)
            //            {
            //                //MessageBox.Show(this, "Converting of file has failed!", "Converting failed!", MessageBoxButtons.OK, MessageBoxIcon.Error);

            //                return;
            //            }
            //        }

            //        ECGWriter.Write(writeFile, "test.xml", true);

            //        if (writeFile != CurrentECG)
            //            writeFile.Dispose();

            //        if (ECGWriter.getLastError() != 0)
            //        {
            //            //MessageBox.Show(this, ECGWriter.getLastErrorMessage(), "Writing of file failed!", MessageBoxButtons.OK, MessageBoxIcon.Error);
            //        }
            //    }
            //}
            //catch (Exception ex)
            //{
            //    //MessageBox.Show(this, ex.ToString(), ex.Message, MessageBoxButtons.OK, MessageBoxIcon.Error);
            //}
        }
    }
}
